﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace createFamen
{
    class Process
    {
        public void Run()
        {
            Console.WriteLine("Hello");

            // 处理实际曲线值，取毫秒的平均值.
            GetSecondData("Sample10605110.txt", "HandledSecond.txt");


            // 显示曲线，观察一下

            // 找到对应的降压阶段. 按照控制阀门逻辑，写出阀门控制.

            // 生成3个txt  工艺曲线.txt  实际曲线.txt  阀门控制.txt

            
        }


        // 写文件
        /// <summary>
        /// 把List容器写入文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="strList">容器名字</param>
        private void WriteFile(string path,
                                        List<String> strList)
        {
            FileStream fs = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);


            int nNum = strList.Count;
            for (int i = 0; i < nNum; i++)
            {
                String each = strList[i];

                sw.WriteLine(each);
            }

            sw.Flush();

            sw.Close();
            fs.Close();
        }

        /// <summary>
        /// 接受原始的毫秒级数据，转化为秒级的数据
        /// </summary>
        /// <param name="incoming"></param>
        /// <param name="outgoing"></param>
        private void GetSecondData(string incoming, string outgoing)
        {
            List<String> incomingList = ReadFile(incoming);


            // 除去最后的毫秒级部分
            List<String> noMillList = HandleMill(incomingList);


            // 取第一个时间作为当前参考时间
            // 如果出现和这个时间不同的时间点，则把
            // 第一个值加入list

            List<String> secondList = HandleSecond(noMillList);

            WriteFile(outgoing, secondList);


        }

        /// <summary>
        /// 把毫秒级数据convert为秒级数据
        /// </summary>
        /// <param name="noMillList"></param>
        /// <returns></returns>
        private List<string> HandleSecond(List<string> noMillList)
        {
            List<string> listRtn = new List<string>();


            string currentTime = GetTime(noMillList[0]);
            string value = GetValue(noMillList[0]);
            listRtn.Add(value);

            for (int i = 0; i < noMillList.Count; i++)
            {

                string compareTime = GetTime(noMillList[i]);
                value = GetValue(noMillList[i]);

                // 每一个当前的值和剩下值做对比.
                // 如果出现和这个时间不同的时间点，则把
                // 第一个值加入list
                if (currentTime != compareTime)
                {
                    listRtn.Add(value);
                    currentTime = compareTime;
                }
                else
                {
                    // do nothing
                }
            }


            return listRtn;
        }

        /// <summary>
        /// 获得值 第一个值
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private string GetValue(string p)
        {
            string[] parts = p.Split(' ');

            return parts[0];
        }

        /// <summary>
        /// 获得时间 第二个值
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private string GetTime(string p)
        {
            string[] parts = p.Split(' ');

            return parts[1];
        }

        /// <summary>
        /// 除去最后的毫秒级部分
        /// 并生成新的List
        /// </summary>
        /// <param name="incomingList"></param>
        /// <returns></returns>
        private List<string> HandleMill(List<string> incomingList)
        {
            List<string> rtnList = new List<string>();

            foreach (string each in incomingList)
            {
                // 删除最后一个冒号和冒号后面的内容
                // 对part2部分重新组合
                string[] ItemParts = each.Split(' ');
                string part2 = ItemParts[1];

                string[]  parts = part2.Split(':');

                string newEach = string.Format("{0} {1}:{2}:{3}",
                                                ItemParts[0],
                                                parts[0],
                                                parts[1],
                                                parts[2]);

                rtnList.Add(newEach);

            }

            return rtnList;
        }

        /// <summary>
        /// 读取文件，提取所有String
        /// </summary>
        /// <param name="incoming"></param>
        /// <returns></returns>
        private List<string> ReadFile(string Path)
        {
            List<string> listRtn = new List<string>();

            StreamReader sr = new StreamReader(Path);

            String line;
            // Read and display lines from the file until the end of 
            // the file is reached.
            while ((line = sr.ReadLine()) != null)
            {
                // 加入书包.
                listRtn.Add(line);

                //Console.WriteLine(line);
            }

            sr.Close();
            sr.Dispose();

            return listRtn;
        }
    }
}
